//获取应用实例
var app = getApp();
var common = require('../../utils/common.js')
Page({
    onLoad: function (options) {
    wx.showLoading({
       title: '加载中',
     });
   },
  onPullDownRefresh: function(){
    this.onShow();
    setTimeout(function(){
      wx.stopPullDownRefresh();
    },1000)
  },
  onShow:function(){
     app.globalData.kehu_back = '';
     var that = this;
     that.getUserList(that);
  },
   getUserList:function(that){
    var user_id = wx.getStorageSync('user_id');
    wx.request({
       url: app.globalData.base + '/Home/PerSon/gerUserInfo',
        method: 'post',
        data: {user_id:user_id},
        header: {
          'Content-Type': 'application/x-www-form-urlencoded'
        },
    success:function(res){
      wx.hideLoading();
      if(res.data.status == '1'){
         that.setData({
          userList:res.data.data,
          status:'1'
         })
      }else{
      	console.log('获取用户失败');
      }
    },
    });
   },
   jixu:function(e){
     var data = e.currentTarget.dataset;
     if(data.kehustep == '0'){
      //设置全局变量
      app.globalData.kehu_id=data.kehuid;
       //添加客户1
      wx.switchTab({
         url: '../../pages/addCustomer1/addCustomer1',
      })
     }else if(data.kehustep == '1'){
        wx.redirectTo({
         url: '../../pages/addCustomer2/addCustomer2?kehu_id='+data.kehuid,
       })
     }else{
       // 添加客户3
      wx.redirectTo({
         url: '../../pages/addCustomer3/addCustomer3?kehu_id='+data.kehuid,
      })
     }
   },
   delkehu:function(e){
    var that = this;
    var kehu_id = e.currentTarget.dataset.kehuid;
    wx.showModal({
            title: '提示',
            content: '确认删除吗',
            success: function (res) {
                if (res.confirm) {
                    wx.request({
                       url: app.globalData.base + '/Home/PerSon/delkehu',
                        method: 'post',
                        data: {kehu_id:kehu_id},
                        header: {
                          'Content-Type': 'application/x-www-form-urlencoded'
                        },
                    success:function(res){
                      if(res.data.status == '1'){
                        wx.showToast({
                          title: res.data.message,  
                          icon:'success', 
                          duration: 2000,
                          success: function () { 
                            that.onShow();
                          },
                        });
                      }else{
                        console.log('获取用户失败');
                      }
                    },
                    });
                }else{
                   console.log('用户点击取消')
                }

            }
        })
   },
   onUnload:function(){
     app.globalData.kehu_back = '1';
   },
})